package com.huabo.system.controller;

import com.hbfk.util.IpUtil;
import com.hbfk.util.JedisUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ValidateCode;
import com.huabo.system.service.TblStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/login")
@Slf4j
@Api(value="LoginController",tags={"登录接口"})
public class LoginController {
	
	
	@Resource
	private TblStaffService tblStaffService;

	/**
	 * 生成验证码
	 * @throws IOException 
	 */
	@ApiOperation("生成验证码")
	@RequestMapping(value ="/getVerificationCode", method = {RequestMethod.GET})
	public void getVerificationCode(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ServletOutputStream out = null;
		Jedis jedis = null;
		log.info("登录页面访问生成验证码");
		try {
			// 设置response，输出图片客户端不缓存
			response.setDateHeader("Expires", 0);
			response.addHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");
			response.setContentType("image/jpeg");
			out = response.getOutputStream();
			ValidateCode vservice = new ValidateCode();
			String code = vservice.getCode();
			String ip = IpUtil.getIpAddr(request);
			log.info("ip: "+ip+",rightCode: "+code);
			jedis = JedisUtil.getJedis();
			jedis.set(ip, code);
			vservice.write(out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(out != null) {
				out.close();
			}
			if(jedis != null) {
				jedis.close();
			}
		}
	}
	
//	/**
//	 * 登录验证用户名验证码 和密码
//	 */
//	@RequestMapping("/loginCheck")
//	public String loginCheck(HttpServletRequest request,@RequestParam(value="username",required=true)String userName,@RequestParam(value="password",required=true)String password,
//			@RequestParam(value="loginType",required=false)String loginType,@RequestParam(value="verifyCode",required=true)String verifyCode,@CookieValue(VERIFY_ID) String verid ) {
//		Map<String,Object> resultMap = new HashMap<String, Object>(0);
//		String result = null;
//		try {
//			resultMap = this.tblStaffService.dealLoginSystem("admin","123456",loginType,verifyCode,"");
//			JSONObject jsonObj = new JSONObject(resultMap);
//			result = jsonObj.toString();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//		return result;
//	}

	/**
	 * 登录验证用户名验证码 和密码
	 */
	@RequestMapping(value ="/loginCheck", method = {RequestMethod.POST})
	@ApiOperation(value = "设置登录方法")
	public JsonBean loginCheck(HttpServletRequest request,HttpServletResponse response, @ApiParam(name="userName",value="用户名",required=true)String userName, @ApiParam(name="password",value="密码",required=true)String password,
			 @ApiParam(name="loginType",value="登录页面类型",required=false)String loginType ) {
		JsonBean jsonBean = null;
		try {
			String ip = IpUtil.getIpAddr(request);
			jsonBean = this.tblStaffService.dealLoginSystem(userName,password,loginType,ip);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	
	/*给OA提供的单点登录接口*/
	@RequestMapping(value ="/oaLoginCheck", method = {RequestMethod.GET})
	@ApiOperation(value = "给OA提供的单点登录接口")
	public JsonBean oaLoginCheck(HttpServletRequest request,HttpServletResponse response, @ApiParam(name="userName",value="用户名",required=true)String userName) {
		JsonBean jsonBean = null;
		try {
			String ip = IpUtil.getIpAddr(request);
			jsonBean = this.tblStaffService.dealOaLoginSystem(userName,ip);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
}
